<template>
  <div>
    <el-card class="box-card">
      <div class="BigDiv">
        <el-select class="ElInput" v-model="billState">
          <el-option label="全部" :value="''"></el-option>
          <el-option label="待缴费" :value="1"></el-option>
          <el-option label="已缴费" :value="2"></el-option>
        </el-select>
        <el-button icon="el-icon-search" type="primary" @click="getBill()"></el-button>
          <el-button type="success" @click="toAddBill()">添加</el-button>
        <!--表格-->
        <el-table
            :data="tableData"
            height="250"
            border
            style="margin-top: 10px;width: 100%">
          <el-table-column
              prop="billRoomNumber"
              label="房间号"
              width="180">
          </el-table-column>
          <el-table-column
              prop="billType"
              label="收费类型"
              width="180">
            <template slot-scope="scope">
              {{scope.row.billType == 0?"业务费": scope.row.billType == 1? "水费": scope.row.billType == 2? "电费": scope.row.billType == 3? "燃气费":"类型"}}
            </template>
          </el-table-column>
          <el-table-column
              prop="billNum"
              label="单价">
          </el-table-column>
          <el-table-column
              prop="billStartTime"
              label="收费开始时间">
          </el-table-column>
          <el-table-column
              prop="billDate"
              label="收费节期">
          </el-table-column>
          <el-table-column
              prop="totalCost"
              label="费用合计">
          </el-table-column>
          <el-table-column
              prop="billState"
              label="缴费状态">
            <template slot-scope="scope">
              {{scope.row.billState == 1?"待缴费": scope.row.billState == 2? "已缴费":"已缴费"}}
            </template>
          </el-table-column>
          <el-table-column
              prop="billOverdueFine"
              label="滞纳金">
          </el-table-column>
          <el-table-column
              fixed="right"
              label="操作">
            <template slot-scope="scope">
              <el-button type="primary" @click="toUpdate(scope.row)">修改</el-button>
              <el-button type="warning" @click="delBill(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--表格-->
      </div>
    </el-card>
    <!--分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page=pageRquset.pageNum
        :page-sizes="[5, 10, 20, 30]"
        :page-size=pageRquset.pageSize
        layout="total, sizes, prev, pager, next, jumper"
        :total=total>
    </el-pagination>
    <!--分页-->
    <!-- 增改弹窗 -->
    <!-- Form -->
    <el-dialog title="增改"
               :visible.sync="dialogFormVisible"
               :before-close="closeExpertFormDialog">
      <el-form :model="billForm" ref="ruleForm">
        <el-form-item label="账单名称">
          <el-input v-model="billForm.billName"></el-input>
        </el-form-item>
        <el-form-item label="房间号">
          <el-input v-model="billForm.billRoomNumber"></el-input>
        </el-form-item>
        <el-form-item label="物业费">
          <el-select v-model="billForm.billType">
            <el-option label="业务费" :value="0"></el-option>
            <el-option label="水费" :value="1"></el-option>
            <el-option label="电费" :value="2"></el-option>
            <el-option label="燃气费" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单价">
          <el-input v-model="billForm.billNum"></el-input>
        </el-form-item>
        <el-form-item label="缴费状态">
          <el-select v-model="billForm.billState">
            <el-option label="待缴费" :value="1"></el-option>
            <el-option label="已经缴费" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
              v-model="billForm.billStartTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
              v-model="billForm.billDate"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdateBill('insert')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 增改弹窗 -->
    <!-- Form -->
  </div>
</template>

<script>
export default {
  /*Data*/
  data() {
    return {
      billState:'',
      pageRquset: {
        pageSize: 5,
        pageNum: 1
      },
      billForm:{
        id:0,
        billName:'',
        billType:'',
        billRoomNumber:'',
        billNum:'',
        billOrderNumber:'',
        billDate:'',
        billState:'',
        totalCost:'',
        billStartTime:'',
        billOverdueFine:'',
        createTime:'',
        updateTime:'',
        billDelFlag:'',
      },
      restBillForm:{
        id:0,
        billName:'',
        billType:'',
        billRoomNumber:'',
        billNum:'',
        billOrderNumber:'',
        billDate:'',
        billState:'',
        totalCost:'',
        billStartTime:'',
        billOverdueFine:'',
        createTime:'',
        updateTime:'',
        billDelFlag:'',
      },
      currentPage2: 1,
      dialogFormVisible:false,
      total:0,
      input: '',
      tableData: [],
    }
  },
  /*自动挂载*/
  mounted() {
    this.getBill()
  },
  /*函数*/
  methods:{
    /*账单全查*/
    getBill () {
      this.$axios.get('/memberLwh/bill/getBill', {
        params: {
          billState: this.billState,
          pageSize: this.pageRquset.pageSize,
          pageNum: this.pageRquset.pageNum
        }
      }).then(res => {
        this.tableData = res.data.obj.content
        this.total = res.data.obj.total
      }).catch(error => {
        console.log(error)
      })
    },
    //添加按钮
    toAddBill(){
      this.dialogFormVisible = true
      this.billForm = {
        id: 0
      }
    },
    // 添加点击X关闭是清空from表单数据
    closeExpertFormDialog (done) {
      this.$refs.ruleForm.resetFields()
      done()// done 用于关闭 Dialog
      this.billForm = {}
    },
    // 添加弹窗里的取消按钮 点击取消是清空from表单数据
    resetForm (formName) {
      this.getBill();
      this.$refs[formName].resetFields()
      this.dialogFormVisible = false
      this.billForm = {}
    },
    // 修改的回显
    toUpdate (bill) {
      this.billForm = bill
      this.dialogFormVisible = true
    },
    /*分页*/
    handleSizeChange (val) {
      this.pageRquset.pageSize = val
      this.getBill()
    },
    handleCurrentChange (val) {
      this.pageRquset.pageNum = val
      this.getBill()
    },
    // 账单增改
    saveOrUpdateBill (formName) {
      // const formData = new FormData()
      this.$axios.post('/memberLwh/bill/saveOrUpdateBill', this.billForm
      ).then(res => {
        if (res.data.code === 200) {
          this.dialogFormVisible = false
          this.billForm = this.restBillForm
          alert('增改成功')
          this.getBill()
        } else {
          alert('增改失败')
          this.insertLog("账单增改")
          this.getBill()
        }
      })
    },
    /*账单删除*/
    delBill (id) {
      debugger
      this.$confirm('是否删除这条数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete('/memberLwh/bill/delBill', {
          params: {
            id: id
          }
        }).then(res => {
          if (res.data.code === 200) {
            this.MyMessage('删除成功', 'sussess')
            this.getBill()
          } else {
            this.MyMessage('删除失败', 'warning')
          }
        }).catch(res => {
          this.MyMessage('删除失败,请联系管理员', 'error')
          this.insertLog("账单删除")
        })
      }).catch(() => {
        this.MyMessage('已取消删除', 'info')
      })
    },
    /*提示框*/
    MyMessage (msg, type) {
      this.$notify({
        showClose: true,
        message: msg,
        type: type
      })
    },
  }
}
</script>

<style>
*{
  margin: 0;
  padding: 0;
}
.ElInput{
  width: 200px;
}
.box-card{
  height: 820px;
  margin: 10px;
}
</style>